using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Cards.Objects;
using Cards.GUI;

namespace Cards.Util
{
    class ArrayUtil
    {
        public static void SwapArrayElements(int[] _array, int _idx1, int _idx2)
        {
            int tmp = _array[_idx1];
            _array[_idx1] = _array[_idx2];
            _array[_idx2] = tmp;
        }

        public static void SwapArrayElements(ArrayList _array, int _idx1, int _idx2)
        {
            int tmp = (int)_array[_idx1];
            _array[_idx1] = _array[_idx2];
            _array[_idx2] = tmp;
        }

        public static void DecreaseSortArray(int[] _array)
        {
            for (int i = 0; i < _array.Length-1; i++)
            {
                for (int j = i + 1; j < _array.Length; j++)
                {
                    if ((int)_array[i]< (int)_array[j])
                    {
                        SwapArrayElements(_array, i, j);
                    }
                }
            }
        }

        public static void DecreaseSortArray(INS_Card[] _array)
        {
            for (int i = 0; i < _array.Length - 1; i++)
            {
                for (int j = i + 1; j < _array.Length; j++)
                {
                    if (_array[i].CARD.CARDTYPE < _array[j].CARD.CARDTYPE)
                    {
                        SwapArrayElements(_array, i, j);
                    }
                }
            }
        }

        public static void SwapArrayElements(INS_Card[] _array,
                                            int _idx1, int _idx2)
        {
            //Swap objects
            INS_Card tmp = _array[_idx1];
            _array[_idx1] = _array[_idx2];
            _array[_idx2] = tmp;

            //Swap location
            int xTmp = _array[_idx1].X;
            int yTmp = _array[_idx1].Y;
            _array[_idx1].X = _array[_idx2].X;
            _array[_idx1].Y = _array[_idx2].Y;
            _array[_idx2].X = xTmp;
            _array[_idx2].Y = yTmp;
        }

        public static void CopyArray(int[] _src, int[] _des)
        {
            for (int i = 0; i < _src.Length; i++)
            {
                _des[i] = _src[i];
            }
        }

        public static Boolean CheckASCArray(ArrayList _array)
        {
            for (int i = 0; i < _array.Count-1; i++)
            {
                if((((INS_Card)_array[i]).CARD.CARDVALUE >= ((INS_Card)_array[i+1]).CARD.CARDVALUE)
                    || (((INS_Card)_array[i+1]).CARD.CARDVALUE - ((INS_Card)_array[i]).CARD.CARDVALUE != 1))
                {
                    return false;
                }
            }
            return true;
        }

        public static Boolean CheckDESCArray(ArrayList _array)
        {
            for (int i = 0; i < _array.Count - 1; i++)
            {
                if ((((INS_Card)_array[i]).CARD.CARDVALUE
                    <= ((INS_Card)_array[i+1]).CARD.CARDVALUE)
                    || (((INS_Card)_array[i]).CARD.CARDVALUE - ((INS_Card)_array[i+1]).CARD.CARDVALUE != 1))
                {
                    return false;
                }
            }
            return true;
        }

        public static int Max(int a, int b)
        {
            return (a > b ? a : b);
        }

        public static int Max(ArrayList _array)
        {
            int max = ((INS_Card)_array[0]).CARD.CARDTYPE;
            for(int i=1;i< _array.Count;i++)
            {
                if (((INS_Card)_array[i]).CARD.CARDTYPE > max)
                {
                    max = ((INS_Card)_array[i]).CARD.CARDTYPE;
                }
            }
            return max;
        }

        public static Boolean FindCardByCardType(ArrayList _arr, int _value)
        {
            for (int idx = 0; idx < _arr.Count; idx++)
            {
                if (((INS_Card)_arr[idx]).CARD.CARDTYPE == _value)
                {
                    return true;
                }
            }
            return false;
        }

        public static Boolean FindCardByCardType(INS_Card[] _arr, int _value)
        {
            for (int idx = 0; idx < _arr.Length; idx++)
            {
                if (_arr[idx].CARD.CARDTYPE == _value)
                {
                    return true;
                }
            }
            return false;
        }

        public static int FindMinCard(INS_Player _player)
        {
            int min = _player.IC[0].CARD.CARDTYPE;

            for (int j = 1; j < _player.NUMOFCARDS; j++)
            {
                if (_player.IC[j].CARD.CARDTYPE < min)
                {
                    min = _player.IC[j].CARD.CARDTYPE;
                }
            }

            return min;
        }

        public static void ASCSortArrayListInt(ArrayList _array)
        {
            for (int i = 0; i < _array.Count - 1; i++)
            {
                for (int j = i + 1; j < _array.Count; j++)
                {
                    if ((int)_array[i] > (int)_array[j])
                    {
                        SwapArrayElements(_array, i, j);
                    }
                }
            }
        }
    }
}
